package com.matt.service.impl;

import com.matt.foundation.vo.CountTransferVo;
import com.matt.repository.CountProductRepository;
import com.matt.service.CountTransferService;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;




@Service
@Transactional
public class CountTransferServiceImpl implements CountTransferService {
    @Autowired
    private CountProductRepository countProductRepository;
    
    private static int a =0;

	@Override
	public CountTransferVo countLeadsTransferClient(String staffId,String areaId) {
		
		List<CountTransferVo> list = countProductRepository.countLeadsTransferClient(staffId,areaId);
		
		StringBuilder leadsId = new StringBuilder();
		list.forEach(l ->{
			leadsId.append("'"+l.getLeadsId()+"',");
		});
		
		CountTransferVo ct = new CountTransferVo();
		if(leadsId.length()>0) {
			
			leadsId.deleteCharAt(leadsId.length()-1);
			
			List<CountTransferVo> list1 = countProductRepository.countContractByStaffId(staffId, leadsId,areaId);
			ct.setCountNum(BigInteger.valueOf(list1.size()));
			ct.setCountTotal(BigInteger.valueOf(list.size()));
						
			return ct;
		}else {
			ct.setCountNum(BigInteger.valueOf(0));
			ct.setCountTotal(BigInteger.valueOf(list.size()));
			return ct;
		}
		
		
		
		
		
	}


	@Override
	public CountTransferVo countLeadsAndRrrive(String staffId,String areaId) {
		
		List<CountTransferVo> list = countProductRepository.countLeadsTransferClient(staffId,areaId);
		
		List<CountTransferVo> leadsList = countProductRepository.countLeadscCurrentMonth(staffId,areaId);
		
		if(leadsList.size()>0) {
			leadsList.get(0).setCountNum(BigInteger.valueOf(list.size()));
			return leadsList.get(0);
		}else {
			CountTransferVo ct = new CountTransferVo();
			ct.setCountNum(BigInteger.valueOf(0));
			ct.setCountTotal(BigInteger.valueOf(0));
			return ct;
		}
		
	}


	@Override
	public CountTransferVo countContractPrice(String staffId,String areaId) {
		
		List<CountTransferVo> list = countProductRepository.countContractPrice(staffId,areaId);
		
		if(list.size()>0) {
			if(null == list.get(0).getCountPrice()) {
				list.get(0).setCountPrice(new BigDecimal("0"));
			}
			return list.get(0);
		}else {
			CountTransferVo ct = new CountTransferVo();
			ct.setCountPrice(new BigDecimal("0"));
			ct.setCountTotal(BigInteger.valueOf(0));
			return ct;
		}
	}


	@Override
	public CountTransferVo countRenewalContractPrice(String staffId,String areaId) {
		
		List<CountTransferVo> list = countProductRepository.countRenewalContract(staffId,areaId);
		
		if(list.size()>0) {
			if(null == list.get(0).getCountPrice()) {
				list.get(0).setCountPrice(new BigDecimal("0"));
			}
			return list.get(0);
		}else {
			CountTransferVo ct = new CountTransferVo();
			ct.setCountPrice(new BigDecimal("0"));
			ct.setCountTotal(BigInteger.valueOf(0));
			return ct;
		}
		
	}


	@Override
	public String countAverageTime(String staffId,String areaId) {
		
		List<CountTransferVo> list = countProductRepository.countAverageTime(staffId);
		
		
		list.forEach(l  ->{
			a += (int)(l.getContractCreated().getTime() - l.getLeadsCreated().getTime());

		});

		String time = "";
		if(a>0 && list.size()>0) {
			 time = MillisecondToDate((int)Math.ceil(a/(1000*list.size())));
		}else {
			time = "0时0分钟0秒";
		}
		
		return time;
	}
    
	private String MillisecondToDate(int time) {
		
		int remainder ;
		int miniRemainder ;
		int hourRemainder ;
		
		if(time > 60 && time<3600) {
			remainder = time%60;
			
			return "0时"+ (time-remainder)/60+"分钟"+remainder+"秒";
		}else if(time>=3600){
			hourRemainder = time%3600;
			miniRemainder = hourRemainder%60;
			
			return (time-hourRemainder)/3600+"时"+ (hourRemainder-miniRemainder)/60+"分钟"+miniRemainder+"秒";
		}else {
			return "0时0分钟"+time+"秒";
		}
		
	}


	@Override
	public CountTransferVo countStayRenewalContractPrice(String staffId,String areaId) {

		List<CountTransferVo> list = countProductRepository.countStayRenewalContract(staffId);
		
		List<CountTransferVo> list1 = countProductRepository.countRenewalContract(staffId,areaId);
		
		
		if(list.size()>0) {
			list1.get(0).setCountNum(BigInteger.valueOf(list.size()));
			return list1.get(0);
		}else {
			list1.get(0).setCountNum(BigInteger.valueOf(0));
			return list1.get(0);
		}
		
		
	}
    
	
  
}
